//
//  LEELoginView.swift
//  SinaWeibo
//
//  Created by 李江波 on 2016/11/13.
//  Copyright © 2016年 lijiangbo. All rights reserved.
//

import UIKit
import SnapKit

@objc protocol LEELoginViewDelegate : NSObjectProtocol {

    @objc optional func registerClick()
    
    func loginClick()
    
}


class LEELoginView: UIView {

    weak var  delegate : LEELoginViewDelegate?
    
    //提供方法供外界调用
    func updateInfo(title : String, imageName : String, isHome : Bool =  false) {
        
        titleLab.text = title
        cricleView.image = UIImage(named: imageName)
        if isHome {
            
            setAnimation()
            
        }else{
            
            bgImg.isHidden = true
            houseImg.isHidden = true
        }
    
    }
    
    /// 动画
    private func setAnimation() {
        let basicAnimator = CABasicAnimation(keyPath: "transform.rotation")
        
        basicAnimator.repeatCount = MAXFLOAT
        basicAnimator.toValue = M_PI * 2
        basicAnimator.duration = 50
        basicAnimator.isRemovedOnCompletion = false
        
        cricleView.layer.add(basicAnimator, forKey: nil)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        registerBtn.addTarget(self, action: #selector(registerClick), for: .touchUpInside)
        loginBtn.addTarget(self, action: #selector(loginClick), for: .touchUpInside)
        
    }
    
    //注册登录按钮点击事件
    @objc private func registerClick() {
        
        delegate?.registerClick?()
    }
    @objc private func loginClick() {
        
        delegate?.loginClick()
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setupUI() {
        
        addSubview(cricleView)
        addSubview(bgImg)
        addSubview(houseImg)
        addSubview(titleLab)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        //控件约束
        cricleView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-80)
        }
        
        bgImg.snp.makeConstraints { (make) in
            make.center.equalTo(cricleView)
        }
        
        houseImg.snp.makeConstraints { (make) in
            make.center.equalTo(cricleView)
        }
        
        titleLab.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.top.equalTo(cricleView.snp.bottom).offset(30)
            
        }
        
        registerBtn.snp.makeConstraints { (make) in
            make.left.equalTo(titleLab)
            make.top.equalTo(titleLab.snp.bottom).offset(20)
            make.width.equalTo(80)
        }
        
        loginBtn.snp.makeConstraints { (make) in
            make.right.equalTo(titleLab)
            make.top.equalTo(titleLab.snp.bottom).offset(20)
            make.width.equalTo(80)

        }
        
        self.backgroundColor = UIColor(white: 237/255.0, alpha: 1)
    }
    
    //懒加载
    
    //阴影遮罩
    lazy var bgImg = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))
    
    /// 转圈圈视图
    lazy var cricleView: UIImageView = {
        let cricleV = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_smallicon"))
        
        return cricleV
    }()
    
    /// 房子
    lazy var houseImg: UIImageView = {
        let houseImg = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_house"))
        
        return houseImg
    }()
    
    /// 文字
    lazy var titleLab: UILabel = {
        let titleLab = UILabel(title: "客官赶快来哦，快点来注册。登录啊快点来注册。登录啊", font: 14)
        
        titleLab.numberOfLines = 0
        //以后设置文字换行,不要使用设置左右边距来限制宽度,不要直接设置约束宽度
        //设置左右: 在计算非常精确的文本自动换行的时候 会导致高度计算的不准确
        titleLab.preferredMaxLayoutWidth = 220
        return titleLab
    }()
    
    /// 注册按钮
    lazy var registerBtn: UIButton = {
        let register = UIButton(title: "注册", backImageName: "common_button_white", titleColor: UIColor.darkGray)
        return register
    }()
    
    /// 登录按钮
    lazy var loginBtn: UIButton = {
        let loginBtn = UIButton(title: "登录", backImageName: "common_button_white", titleColor: UIColor.orange)
        return loginBtn
    }()
    
    
}










